/*
 * @Author: 黄承文 chengwen@ssc-hn.com
 * @Date: 2023-02-17 14:02:48
 * @LastEditors: 黄承文 chengwen@ssc-hn.com
 * @LastEditTime: 2023-02-17 16:07:48
 * @FilePath: /sscd/src/router/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { App } from 'vue';
import { createRouter, RouteRecordRaw, createWebHashHistory } from 'vue-router';
import { loadDemo } from '@/service/packages';

const history = createWebHashHistory();
export const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    redirect: '/list',
    // component: () => import('@/views/home/index.vue'),
  },
  {
    path: '/list',
    name: 'List',
    component: () => import('@/views/list/index.vue'),
  },
  {
    path: '/doc/:name',
    name: 'Doc',
    component: () => import('@/views/doc/index.vue'),
  },
  {
    path: '/package/:name',
    name: 'Package',
    component: () => import('@/views/package/index.vue'),
  },
  {
    path: '/:path(.*)*',
    name: 'error404',
    component: () => import('@/views/error-page/404.vue'),
  },
];
const router = createRouter({
  history,
  routes,
  strict: true,
});

export async function setupRouter(app: App) {
  router.beforeEach(async (to, from, next) => {
    if (to.name === 'Package') {
      await loadDemo(to.params.name as string, app);
      next();
    } else {
      next();
    }
  });
  app.use(router);
}
export default router;
